//------------------------------------------------------------------------------
// <copyright file="RpcError.cpp" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

#include "stdafx.h"

#ifdef ENABLE_GAMESTEST_RPC

#include "RpcError.h"

namespace Microsoft { namespace Internal { namespace GamesTest { namespace Rpc { namespace Server
{
	void RpcPrintError(HRESULT hr)
	{
		OutputDebugString(L"TDK RPC: ");
		const int BufferSize = 1024;  
		WCHAR buffer[BufferSize];
		DWORD stringLength = 0;

#ifdef _WINRT_DLL
		HMODULE thisModule = LoadPackagedLibrary(TARGET_NAME, 0);

		stringLength = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM, thisModule, hr, 0, buffer, BufferSize, nullptr);
		
		if (thisModule != nullptr)
		{
			FreeLibrary(thisModule);
		}
#else
		stringLength = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, hr, 0, buffer, BufferSize, nullptr);
#endif

		if(stringLength > 0)
		{
			OutputDebugString(buffer);
			OutputDebugString(L"\n");
		}
		else
		{
			swprintf(buffer, _countof(buffer), L"Unknown error code encountered: %d\n", hr);
			OutputDebugString(buffer);
		}
	}

	void RpcPrintMessage(const wchar_t * msg)
	{
		OutputDebugString(L"TDK RPC: ");
		OutputDebugString(msg);
		OutputDebugString(L"\n");
	}

	void RpcPrintMessage(const char * msg)
	{
		OutputDebugStringA("TDK RPC: ");
		OutputDebugStringA(msg);
		OutputDebugStringA("\n");
	}

}}}}}

#endif // ENABLE_GAMESTEST_RPC